package com.sphd.emanu.activity.staff;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.emanu.R;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.utils.PermissionConfig;
import com.wzutils.L;
import com.wzutils.MD5Utils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * todo 高管管理 与总务管理共用的界面
 *
 * @Author Hu
 * @Time 2017/7/25 -- 20:21.
 * @Email 87683202@qq.com
 */
public class StaffActivity extends MVPBaseActivity<StaffContract.View, StaffPresenter> implements StaffContract.View {

    @BindView(R.id.save_staff)
    TextView saveStaff;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.staff_name)
    MaterialEditText staffName;
    @BindView(R.id.staff_phone)
    MaterialEditText staffPhone;
    @BindView(R.id.del_staff)
    Button delStaff;
    private AndroidNextInputs inputs;
    Intent intent;
    Bundle bundle;
    User user;
    User userStaff;
    int type = 0;//0表示新增 1表示总务的修改  2表示高管的修改
    List<Moudle> moudles;
    boolean isCanDo=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_staff);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
    }

    private void initView() {
        inputs = new AndroidNextInputs();
        inputs.add(staffName, StaticScheme.Required().msgOnFail("姓名不能为空"), StaticScheme.NotBlank().msgOnFail("姓名不能为空"));
        inputs.add(staffPhone, StaticScheme.ChineseMobile());
        intent = getIntent();
        bundle = intent.getExtras();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        moudles= (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo=isCanDo(moudles, PermissionConfig.MENU_ZONGWU_13);
        type = bundle.getInt(Constant.TYPE);
        setTitleBar(toolbar, type == 0 ? "新增" : "编辑");
        if (type != 0) {
            userStaff = (User) bundle.getSerializable(Constant.USER_STAFF);
            staffName.setText(userStaff.getUsername());
            staffPhone.setText(userStaff.getMobile());
            if (type == 1) {
                delStaff.setVisibility(View.VISIBLE);
            }
        }
        if (type!=2){
            if (!isCanDo){
                saveStaff.setVisibility(View.GONE);
                delStaff.setVisibility(View.GONE);
            }
        }

    }

    @OnClick({R.id.save_staff, R.id.del_staff})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.save_staff:
                if (inputs.test()) {
                    if (type == 0) {
                        mPresenter.addUser(staffPhone.getText().toString().trim(), user.getOid(), 5, "派送员",
                                staffPhone.getText().toString().trim(), MD5Utils.MD5(sixString(
                                        staffPhone.getText().toString().trim())), staffName.getText().toString().trim());
                    } else {
                        mPresenter.updateUser(userStaff.getUserid(),
                                userStaff.getLogonname().equals(staffPhone.getText().toString().trim()) ? null :
                                        staffPhone.getText().toString().trim()
                                , user.getOid(),
                                type == 1 ? 5 : Integer.valueOf(userStaff.getRoleid()), type == 1 ? "派送员" :
                                        userStaff.getRolename(),
                                userStaff.getLogonname().equals(staffPhone.getText().toString().trim()) ? null :
                                        staffPhone.getText().toString().trim(),
                                userStaff.getLogonname().equals(staffPhone.getText().toString().trim()) ? null :
                                        MD5Utils.MD5(sixString(staffPhone.getText().toString().trim())),
                                staffName.getText().toString().trim());
                    }
                } else {
                    toast("请按提示要求填写。");
                }
                break;
            case R.id.del_staff:
                if (type == 1) {
                    showProgressDialog();
                    mPresenter.deleteUser(userStaff.getUserid());
                } else {
                    toast("数据展示错误！");
                }

                break;
        }
    }

    private String sixString(String mobile) {
        return mobile.substring(mobile.length() - 6);
    }

    @Override
    public void recRes(int res) {
        //返回值,-1为帐号已存在,0为新增失败,1为成功
        dissProgressDialog();
        if (res == 1) {
            toast("操作成功。");
            setResult(Constant.REQUEST_CODE);
            finish();
        } else if (res == -1) {
            toast("帐号已存在");
        } else if (res == 0) {
            toast("操作失败，请重试！");
        }
    }
}
